package pvt_chat

import (
	"github.com/spf13/cast"
	"longmen/server/config/global"
	"longmen/server/internal/rpc/tencentim/cache"
	"longmen/server/internal/rpc/tencentim/messages/im"
	"sort"
	"strings"
	"time"
)

// SessionType.
const (
	SingleChatType       = 1
	GroupChatType        = 2
	SuperGroupChatType   = 3
	NotificationChatType = 4
)

func SetSingleChatConversationAnchorSend(userId, anchorId, content string) (*im.Conversation, error) {
	return setSingleChatConversation(userId, anchorId, content, "", "", "", false)
}

func SetSingleChatConversationUserReplyFlag(userId, anchorId, content, ip, optPlatform, channel string) (*im.Conversation, error) {
	return setSingleChatConversation(userId, anchorId, content, optPlatform, ip, channel, true)
}

func setSingleChatConversation(userId, anchorId, content, optPlatform, ip, channel string, replyFlag bool) (*im.Conversation, error) {
	var conversation im.Conversation
	conversation.ConversationId = getConversationIDBySessionType(SingleChatType, cast.ToString(userId), cast.ToString(anchorId))
	conversation.UserId = userId
	conversation.LastetMsgText = content
	conversation.AnchorId = anchorId
	conversation.LatestMsgTime = time.Now().Unix()
	conversationCache, _ := GetSingleChatConversation(userId, conversation.ConversationId)
	if conversationCache == nil {
		conversation.CreateTime = time.Now().Unix()
	} else {
		conversation.CreateTime = conversationCache.CreateTime
		conversation.UserIp = conversationCache.UserIp
		conversation.UserAvatar = conversationCache.UserAvatar
		conversation.ReplyFlag = conversationCache.ReplyFlag
		conversation.Channel = conversationCache.Channel
	}
	if channel != "" {
		conversation.Channel = channel
	}
	if ip != "" {
		conversation.UserIp = ip
	}
	if optPlatform != "" {
		conversation.OptPlatform = optPlatform
	}
	if replyFlag {
		conversation.ReplyFlag = true
	}
	err := cache.SetConversationCache(cast.ToString(userId), &conversation)
	if err != nil {
		global.Log.Errorf("CreateSingleChatConversation %v", err)
		return nil, err
	}
	//因为没有好友关系，所以需要各自创建一条
	err = cache.SetConversationCache(cast.ToString(anchorId), &conversation)
	if err != nil {
		global.Log.Errorf("CreateSingleChatConversation %v", err)
		return nil, err
	}
	return &conversation, nil
}

func GetSingleChatConversations(ownerUserId string) ([]*im.Conversation, error) {
	return cache.GetUserAllConversationCache(ownerUserId)
}

func GetSingleChatConversationSendIdToRecvId(ownerUserId, recvId string) (*im.Conversation, error) {
	conversation, err := cache.GetConversationCache(ownerUserId, getConversationIDBySessionType(SingleChatType, cast.ToString(ownerUserId), cast.ToString(recvId)))
	if err != nil {
		global.Log.Errorf("GetSingleChatConversation err:%v", err)
		return nil, err
	}
	return conversation, nil
}

func GetSingleChatConversation(ownerUserId, conversationId string) (*im.Conversation, error) {
	conversation, err := cache.GetConversationCache(ownerUserId, conversationId)
	if err != nil {
		global.Log.Errorf("GetSingleChatConversation err:%v", err)
		return nil, err
	}
	return conversation, nil
}

func getConversationIDBySessionType(sessionType int, ids ...string) string {
	sort.Strings(ids)
	if len(ids) > 2 || len(ids) < 1 {
		return ""
	}
	switch sessionType {
	case SingleChatType:
		return "si_" + strings.Join(ids, "_") // single im
	case GroupChatType:
		return "g_" + ids[0] // group im
	case SuperGroupChatType:
		return "sg_" + ids[0] // super group im
	case NotificationChatType:
		return "sn_" + ids[0] // server notification im
	}
	return ""
}
